package paper.http;

import java.io.PrintWriter;
import java.util.regex.*;

public class Pager {
    PrintWriter out;
    int rowCount;
    int page = 0;
    String uri;
    StringBuilder navigation = new StringBuilder();
    int howManyPages;

    // One can argue whether this should be constant, or some user preference. For now it is constant... Change at your will!

    public final int displayLimit = 20;
    public String addendum = "";

    public Pager(PrintWriter out, int rowCount, String[] pagePar, String uri) {
        this.out = out;
        this.rowCount = rowCount;
        if (pagePar != null) {
            this.page = getPage(pagePar[0]);
        }
        howManyPages = rowCount/displayLimit;
        if (page>howManyPages){
            page=howManyPages;
        }
        if(rowCount>displayLimit){
            addendum=" limit "+displayLimit+" offset "+page*displayLimit+" ";
        }
        this.uri = uri;
        buildFloats();
    }

    public void header() {
        header("","");
    }

    public void header(String noResults, String meta) {
        out.println("<div class='searchinfo' style='padding:5px; border-bottom:dashed 1px black;'>");
        /* Were any results actually returned? */
        if(rowCount<1){
            out.println(noResults);
        } else if(rowCount==1) {
            out.println("One paper");	
        } else {
            out.println(navigation);
            out.println(rowCount+" papers");
        }
        out.println(meta + "</div>");
    }

    public void footer() {
        if(howManyPages>0){
            out.println("<div style='padding:5px; border-top:dashed 1px black; padding-bottom:1.5em;'>");
            out.println(navigation);
            out.println("</div>");
        }
    }

    private void buildFloats() {
        // is there a pleasant way of doing this without tables? Needs style...
        navigation.append("<table width='100%'><tr><td width='31%'>");
        if(page>0){
            navigation.append("<a href='"+uri+"page="+(page-1)+" '>Previous page</a>");
        }
        navigation.append("</td>");
        if (howManyPages != 1) {
            final int window = 5;
            navigation.append("<td width='4%' align='center'>");
            if (page > window) {
                navigation.append("<a href='"+uri+"page=0'>1</a> ...");
            }
            navigation.append("</td>");
            for (int i = page - window; i <= page+window; i++) {
                if (i == page) {
                    navigation.append("<td width='3%' align='center'><b>" + (i+1) + "</b></td>");
                } else if (i >= 0 && i <= howManyPages) {
                    navigation.append("<td width='3%' align='center'><a href='"+uri+"page=" + i +"'>" +
                                      (i+1) + "</a></td>");
                } else {
                    navigation.append("<td width='3%'>&nbsp;</td>");
                }
            }
            navigation.append("<td width='4%' align='center'>");
            if (page < howManyPages - window) {
                navigation.append("... <a href='"+uri+"page="+howManyPages+"'>"+(howManyPages+1)+"</a>");
            }
            navigation.append("</td>");
        }
        navigation.append("<td width='31%' align='right'>");
        if(page<howManyPages){
            navigation.append("<a href='"+uri+"page="+(page+1)+" '>Next page</a></td>");
        }
        navigation.append("</td></tr></table>");
    }

    private int getPage(String pageParameter) {
		int page=0;
        Pattern pattern = Pattern.compile("[0-9]+");
        Matcher matcher = pattern.matcher(pageParameter);
        if(matcher.find()){
            try { page=Integer.parseInt(matcher.group());}
            catch(NumberFormatException e) { }
        }
		return page;
    }

}
